package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.CashRecord;
import com.weipai.springboot.model.Customer;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.CashRecordDao;
import com.weipai.springboot.module.dao.CustomerDao;
import com.weipai.springboot.module.mapper.CashRecordMapper;
import com.weipai.springboot.module.mapper.CustomerMapper;
import com.weipai.springboot.module.service.CashRecordService;
import com.weipai.springboot.module.util.DateUtils;
import com.weipai.springboot.module.util.SessionUserUtil;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author songmengqiang
 * @create 2018-09-26 18:11
 **/
@Service
public class CashRecordServiceImpl implements CashRecordService {

    @Autowired
    private CashRecordDao cashRecordDao;
    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private SessionUserUtil sessionUserUtil;
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    CashRecordMapper cashRecordMapper;

    //添加提现记录
    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Response save(CashRecord cashRecord) {
        Response response = new Response();
        Customer customer = customerMapper.getCustomerById(cashRecord.getCustomerId());
        //验证是否 有提现记录
        CashRecord cashRecord1 = cashRecordMapper.verifyCashRecord(cashRecord.getCustomerId(), 0);
        if (cashRecord1 == null) {
            //判断是否绑定支付宝  否则去绑定
            if (!Strings.isEmpty(customer.getAlipayAccount()) && !Strings.isEmpty(customer.getCustomerRealName())) {
                cashRecord.setAccNo(customer.getAlipayAccount()); //提现账号
                cashRecord.setAccUser(customer.getCustomerRealName());//真实用户名
                cashRecord.setCustomerId(customer.getCustomerId()); //用户id
                cashRecord.setStatus(0); //'提现状态	 -1:提现失败 0:待处理 1:提现成功
                if (1 == cashRecordDao.save(cashRecord)) {
                    response.setCode(RetCode.SUCCESS);
                } else {
                    response.setCode(RetCode.SAVE_ERROR);
                }
            } else {
                response.setCode(RetCode.ALIPAY_NO);
            }
        } else {
            response.setCode(RetCode.CASHRECORD_VER);
        }
        return response;
    }

    @Override
    public Response update(CashRecord cashRecord) {
        Response response = new Response();
        if (1 == cashRecordDao.update(cashRecord)) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    public Response query() {
        Response response = new Response();
        Customer customer = sessionUserUtil.getUserAttr();
        List<CashRecord> list = cashRecordDao.queryByCustomerId(customer.getCustomerId());
        response.put("list", list);
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    //增加银行卡提现记录
    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Response saveBalance(CashRecord cashRecord) {
        Response response = new Response();
        Customer customer = customerMapper.getCustomerById(cashRecord.getCustomerId());
        //验证是否 有提现记录
        CashRecord cashRecord1 = cashRecordMapper.verifyCashRecord(cashRecord.getCustomerId(), 0);
        if (cashRecord1 == null) {
            //判断是否绑定银行卡
            cashRecord.setAccType(3);
            cashRecord.setStatus(0); //'提现状态	 -1:提现失败 0:待处理 1:提现成功
            if (1 == cashRecordDao.save(cashRecord)) {
                response.setCode(RetCode.SUCCESS);
            } else {
                response.setCode(RetCode.SAVE_ERROR);
            }
        } else {
            response.setCode(RetCode.CASHRECORD_VER);
        }
        return response;
    }

    //更改提现审核状态
    @Override
    public Response updateBalance(String cashRecordId, String status) {
        Response response = new Response();
        int cashRecordIdInt = Integer.parseInt(cashRecordId);
        CashRecord cashRecord = cashRecordMapper.selectByPrimaryKey(cashRecordIdInt);
        if (status != null && !"".equals(status)) {
            if ("-1".equals(status)) {
                cashRecord.setStatus(-1);
                if (1 == cashRecordDao.update(cashRecord)) {
                    response.setCode(RetCode.SUCCESS);
                }
            } else {
                cashRecord.setStatus(1);
                if (1 == cashRecordDao.update(cashRecord)) {
                    Customer customer = customerMapper.getCustomerById(cashRecord.getCustomerId());
                    customer.setHcashMoney(customer.getHcashMoney() - cashRecord.getMoney());
                    customerDao.updateByPrimaryKey(customer);
                    response.setCode(RetCode.SUCCESS);
                }
            }
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    public Response staffAudit(Integer status, Integer page, Integer size, String draw, String search, String startTime, String endTime,Integer targetType) {
        Response response = new Response();
        Integer start = (page - 1) * size;
        List<Map<String, Object>> cashRecordMap;
        int cashRecordAmount;
//        Integer targetType1=Integer.parseInt(targetType);
        cashRecordMap = cashRecordMapper.getCashRecordList22(status, start, size, search, startTime, endTime,targetType);
        cashRecordAmount = cashRecordMapper.getCashRecordAmount22(status, search, startTime, endTime,targetType);
        PageResult<Map<String, Object>> pageList = new PageResult<>(cashRecordMap, page, size, cashRecordAmount, draw);
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }

//    @Override
//    public Response artisanAudit(Integer status, Integer page, Integer size, String draw, String search, String startTime, String endTime) {
//        Response response = new Response();
//        Integer start = (page - 1) * size;
//        List<Map<String, Object>> cashRecordMap;
//        int cashRecordAmount;
//        cashRecordMap = cashRecordMapper.getCashRecordList22(status, start, size, search, startTime, endTime);
//        cashRecordAmount = cashRecordMapper.getCashRecordAmount22(status, search, startTime, endTime);
//        PageResult<Map<String, Object>> pageList = new PageResult<>(cashRecordMap, page, size, cashRecordAmount, draw);
//        response.setCode(RetCode.SUCCESS);
//        response.put("pageList", pageList);
//        return response;
//    }


}
